import 'package:flutter/material.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';

class ChooseTypeModel {
  String? name;
  String? type;

  ChooseTypeModel({this.name, this.type});

  ChooseTypeModel.fromJson(Map<String, dynamic> json) {
    name = json["name"];
    type = json["type"];
  }

  static List<ChooseTypeModel> fromList(List<Map<String, dynamic>> list) {
    return list.map(ChooseTypeModel.fromJson).toList();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data["name"] = name;
    _data["type"] = type;
    return _data;
  }
}

class DropDownWidget extends StatefulWidget {
  final List<ChooseTypeModel> dataList;
  final String type;
  final String style;
  Function(String type) onPress;
  final String initialSelection;
  DropDownWidget({
    super.key,
    required this.dataList,
    required this.type,
    required this.onPress,
    this.style = 'normal',
    this.initialSelection = ''
  });

  @override
  State<DropDownWidget> createState() => _DropdownMenuNode1State();
}

class _DropdownMenuNode1State extends State<DropDownWidget> {
  late String? dropdownValue = '请选择';
  // 定义颜色状态变量
  Color currentMenuColor = Colors.blue;
  @override
  Widget build(BuildContext context) {
    return Container(
      clipBehavior: Clip.antiAlias,
      padding: EdgeInsets.only(left: 0.px),
      decoration: BoxDecoration(
        color: widget.style=='normal'?AppColors.backgroundColor :const Color(0xFFF7F7F7),
        borderRadius: BorderRadius.circular(12.px),
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          DropdownMenu(
            inputDecorationTheme: InputDecorationTheme(
              border: InputBorder.none,
              contentPadding: EdgeInsets.only(left: 20.px),
            ),
            menuHeight: 150.px,
            width: 344.px,
            initialSelection: widget.initialSelection,
            onSelected: _onSelect,
            textStyle: TextStyle(
              fontSize: 14.px,
              color: AppColors.fontColorBlack,
            ),
            dropdownMenuEntries: _buildMenuList(widget.dataList),

            menuStyle: MenuStyle(
              backgroundColor: MaterialStateProperty.resolveWith<Color>((
                Set<MaterialState> states,
              ) {
                if (states.contains(MaterialState.hovered)) {
                  return AppColors.backgroundColor;
                }
                return AppColors.backgroundColorGreyF7;
              }),
            ),
          ),
        ],
      ),
    );
  }

  void _onSelect(String? value) {
    setState(() {
      dropdownValue = value!;
      widget.onPress(value);
    });
  }

  List<DropdownMenuEntry<String>> _buildMenuList(List<ChooseTypeModel> data) {
    return data.map((ChooseTypeModel e) {
      return DropdownMenuEntry<String>(value: '${e.type}', label: '${e.name}');
    }).toList();
  }
}
